package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ActivitySummary;
import com.ruoyi.system.domain.MeetingManagement;
import com.ruoyi.system.domain.MeetingSummary;
import com.ruoyi.system.mapper.ActivitySummaryMapper;
import com.ruoyi.system.mapper.MeetingSummaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FileColumnMapper;
import com.ruoyi.system.domain.FileColumn;
import com.ruoyi.system.service.IFileColumnService;
import org.springframework.util.ObjectUtils;

/**
 * 文件栏管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-25
 */
@Service
public class FileColumnServiceImpl implements IFileColumnService
{
    @Autowired
    private FileColumnMapper fileColumnMapper;
    @Autowired
    private MeetingSummaryMapper meetingSummaryMapper;
    @Autowired
    private ActivitySummaryMapper activitySummaryMapper;

    /**
     * 查询文件栏管理
     *
     * @param id 文件栏管理主键
     * @return 文件栏管理
     */
    @Override
    public FileColumn selectFileColumnById(Long id)
    {
        FileColumn fileColumnById = fileColumnMapper.selectFileColumnById(id);
        String fileType = fileColumnById.getFileType();
        if("0".equals(fileType)){
            Long associationTypeId = fileColumnById.getAssociationTypeId();
            if(!ObjectUtils.isEmpty(associationTypeId)){
                MeetingSummary summary = meetingSummaryMapper.selectMeetingSummaryByMeetingId(associationTypeId);
                if(!ObjectUtils.isEmpty(summary)){
                    String summaryImage = summary.getSummaryImage();
                    if(StringUtils.isNotBlank(summaryImage)){
                        List<String> summaryImageList = Arrays.asList(summaryImage.split(","));
                        summary.setSummaryImageList(summaryImageList);
                    }
                }
                fileColumnById.setMeetingSummary(summary);
            }
        }else if("1".equals(fileType)){
            Long associationTypeId = fileColumnById.getAssociationTypeId();
            if(!ObjectUtils.isEmpty(associationTypeId)){
                ActivitySummary summary = activitySummaryMapper.selectActivitySummaryByActivityId(associationTypeId);
                if(!ObjectUtils.isEmpty(summary)){
                    String summaryImage = summary.getSummaryImage();
                    if(StringUtils.isNotBlank(summaryImage)){
                        List<String> summaryImageList = Arrays.asList(summaryImage.split(","));
                        summary.setSummaryImageList(summaryImageList);
                    }
                }
                fileColumnById.setActivitySummary(summary);
            }
        }
        return fileColumnById;
    }

    /**
     * 查询文件栏管理列表
     *
     * @param fileColumn 文件栏管理
     * @return 文件栏管理
     */
    @Override
    public List<FileColumn> selectFileColumnList(FileColumn fileColumn)
    {
        List<FileColumn> fileColumnList = fileColumnMapper.selectFileColumnList(fileColumn);
        for(FileColumn fileColumnVo : fileColumnList){
            String fileType = fileColumnVo.getFileType();
            if("0".equals(fileType)){
                Long associationTypeId = fileColumnVo.getAssociationTypeId();
                if(!ObjectUtils.isEmpty(associationTypeId)){
                    MeetingSummary summary = meetingSummaryMapper.selectMeetingSummaryByMeetingId(associationTypeId);
                    if(!ObjectUtils.isEmpty(summary)){
                        String summaryImage = summary.getSummaryImage();
                        if(StringUtils.isNotBlank(summaryImage)){
                            List<String> summaryImageList = Arrays.asList(summaryImage.split(","));
                            summary.setSummaryImageList(summaryImageList);
                        }
                    }
                    fileColumnVo.setMeetingSummary(summary);
                }
            }else if("1".equals(fileType)){
                Long associationTypeId = fileColumnVo.getAssociationTypeId();
                if(!ObjectUtils.isEmpty(associationTypeId)){
                    ActivitySummary summary = activitySummaryMapper.selectActivitySummaryByActivityId(associationTypeId);
                    if(!ObjectUtils.isEmpty(summary)){
                        String summaryImage = summary.getSummaryImage();
                        if(StringUtils.isNotBlank(summaryImage)){
                            List<String> summaryImageList = Arrays.asList(summaryImage.split(","));
                            summary.setSummaryImageList(summaryImageList);
                        }
                    }
                    fileColumnVo.setActivitySummary(summary);
                }
            }
        }
        return fileColumnList;
    }

    /**
     * 新增文件栏管理
     *
     * @param fileColumn 文件栏管理
     * @return 结果
     */
    @Override
    public AjaxResult insertFileColumn(FileColumn fileColumn)
    {
        fileColumn.setCreateTime(DateUtils.getNowDate());
        //查询轮播序号是否存在
        String fileSort = fileColumn.getFileSort();
        if(StringUtils.isNotBlank(fileSort)){
            FileColumn column = fileColumnMapper.selectFileColumnBySort(fileSort);
            if(!ObjectUtils.isEmpty(column)){
                return AjaxResult.error("序号不能重复");
            }
        }
        fileColumnMapper.insertFileColumn(fileColumn);
        return AjaxResult.success("新增成功");
    }

    /**
     * 修改文件栏管理
     *
     * @param fileColumn 文件栏管理
     * @return 结果
     */
    @Override
    public AjaxResult updateFileColumn(FileColumn fileColumn)
    {
        fileColumn.setUpdateTime(DateUtils.getNowDate());
        //查询轮播序号是否存在
        String fileSort = fileColumn.getFileSort();
        FileColumn fileColumnById = fileColumnMapper.selectFileColumnById(fileColumn.getId());
        String sort = fileColumnById.getFileSort();
        if(StringUtils.isNotBlank(fileSort)&&!fileSort.equals(sort)){
            FileColumn column = fileColumnMapper.selectFileColumnBySort(fileSort);
            if(!ObjectUtils.isEmpty(column)){
                return AjaxResult.error("序号不能重复");
            }
        }
        fileColumnMapper.updateFileColumn(fileColumn);
        return AjaxResult.success("修改成功");
    }

    /**
     * 删除文件栏管理信息
     *
     * @param id 文件栏管理主键
     * @return 结果
     */
    @Override
    public int deleteFileColumnById(Long id)
    {
        return fileColumnMapper.deleteFileColumnById(id);
    }
}
